﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.Common;
using System.Linq;
using System.Text;
using Dataglot;

namespace IrishSettings
{
    public class SelectAllApplicationSettings:GenericCommand
    {
        public SelectAllApplicationSettings(DataFactory factory)
            : base(factory)
        {
            const string sql = @"
SELECT    apps.id application_id, app_schemas.id schema_id, apps.application , apps.version , app_schemas.name , 
                      app_schemas.scope , app_schemas.default_serialized_value , app_schemas.value_type , 
                      app_schemas.is_user , app_schemas.is_anonymous , apps.culture , 
                      settings.serialized_value , settings.value_type 
FROM         {$ns}_settings_applications apps INNER JOIN
                      {$ns}_settings_schema app_schemas ON apps.id = app_schemas.application_id INNER JOIN
                      {$ns}_settings_values settings ON app_schemas.id = settings.schema_id
WHERE 
    apps.application= @application 
    AND apps.version= @version ";
            ComposeSql(sql);
            AddParameter("@application", DbType.String);
            AddParameter("@version", DbType.String);
        }

        public DbDataReader Select(
            string application,
            Version version
            )
        {
            if (string.IsNullOrEmpty(application))
                throw new ArgumentException("Missing application name", "application");
            if (version == null)
                throw new ArgumentException("Missing version", "version");

            command.Parameters["@application"].Value = application;
            command.Parameters["@version"].Value = version.ToString();
            return ExecuteReader(CommandBehavior.SingleResult);
        }
    }
}
